/*******************************************************************************
 * @file Copyright 2009 The CREATE Lab at CMU
 * 
 *       This file is part of JubJub, an open source project.
 *       http://code.google.com/p/jubjub/
 * 
 *       Redistribution and use in source and binary forms, with or without
 *       modification, are permitted provided that the following conditions are
 *       met:
 * 
 *       Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 * 
 *       Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 * 
 *       Neither the name of Carnegie Mellon nor the names of its contributors
 *       may be used to endorse or promote products derived from this software
 *       without specific prior written permission.
 * 
 *       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *       "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *       LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 *       PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *       HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *       SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *       LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *       DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *       THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *       (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *       OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

import java.awt.Frame;
import java.awt.event.MouseEvent;

import processing.core.PApplet;

public class MyProcessingSketch extends PApplet{

   Stripe[] stripes = new Stripe[50];
   DraggableBlock[] blocks = new DraggableBlock[10];
   
   public void setup() 
   {
      frame.setResizable(true);
      size(800,600);
      for(int i = 0; i < stripes.length; i++)
      {
         stripes[i] = new Stripe(this);
      }
      for(int i = 0; i < blocks.length;i++)
      {
         blocks[i] = new DraggableBlock(i,this,System.getProperty("user.dir")+"\\data\\testImg.jpg",
                           "block " + i, (int)random(400),(int)random(400));
      }
   }
   
   public void draw() 
   {
      background(100);
      // Move and display all "stripes"
      for (int i = 0; i < stripes.length; i++) 
      {
         stripes[i].move();
         stripes[i].display();
      }
      for (int i = 0; i < blocks.length; i++)
      {
         blocks[i].display();
      }
   }
   
   public void mousePressed(MouseEvent e)
   {
      if(e.getButton()==MouseEvent.BUTTON1)
      {
         for (int i = blocks.length-1; i >= 0; i--)
         {
            if(blocks[i].contains(e.getX(), e.getY()))
            {
               blocks[i].press(e.getX(), e.getY());
               System.out.println("block " + i + " pressed.");
               break;
            }
         }
      }
   }
   
   public void mouseDragged(MouseEvent e)
   {
      boolean blockDragged = false;
      for (int i = 0; i < blocks.length; i++)
      {
         if(blocks[i].isPressed())
         {
            blocks[i].drag(e.getX(), e.getY());
            blockDragged = true;
            //System.out.println("block " + i + " dragged.");
         }
      }
      if(!blockDragged)
      {
         //e.
      }
   }
   
   public void mouseReleased()
   {
      for (int i = 0; i < blocks.length; i++)
      {
         if(blocks[i].isPressed())
         {
            blocks[i].release();
            System.out.println("block " + i + " released.");
         }
      }
   }
   
   public void mouseClicked(MouseEvent e)
   {
      if(e.getButton()==MouseEvent.BUTTON1)
      {
         if(e.getClickCount() == 2)
         {
            int x = e.getX();
            int y = e.getY();
            
            for(int i = 0; i < blocks.length; i++)
            {
               if(blocks[i].contains(x, y))
               {
                  return;
               }
            }
            //turn maximized on or off when double clicked not on an object.
            if(frame.getExtendedState() != Frame.MAXIMIZED_BOTH)
            {
               frame.setExtendedState(Frame.MAXIMIZED_BOTH);
            }
            else
            {
               frame.setExtendedState(Frame.NORMAL);
            }
         }
      }
   }
   
   public static void main(String args[]) 
   {
      PApplet.main(new String[] { "MyProcessingSketch" });
   }

}
